package com.financial.token.utils;

import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.financial.token.utils.apiresult.ApiResult;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtUtils {

    @Autowired
    private RedisUtils redisUtils;
    /**
     * 解密
     *
     * @param jwt
     * @param classT
     * @param <T>
     * @return //解密，传入一个加密后的token字符串和解密后的类型
     */
    //private static final String secret = "2d7204ac93024094ae07e6d953dcd86b";
    private static final String EXP = "exp";
    private static final String PAYLOAD = "payload";

    public static Map<String, Object> unsign(String jwt, String secret) {
        JWTVerifier verifier = new JWTVerifier(secret);
        try {
            Map<String, Object> claims = verifier.verify(jwt);
            return claims;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * https://blog.csdn.net/change_on/article/details/71191894
     * //加密，传入一个对象和有效期
     * wgq
     */
    public static String sign(String secret, long maxAge) {
        try {
            JWTSigner signer = new JWTSigner(secret);
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put(EXP, System.currentTimeMillis() + maxAge);
            return signer.sign(claims);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取accessToken
     * @param requestMap
     * @return
     */
    public String getAccessToken(Map<String,Object> requestMap) {
        Map<String, Object> reqMap = new HashMap<>();

        String platfromMarking = requestMap.get("platfromMarking").toString();
        String username = requestMap.get("username").toString();
        String passWord = requestMap.get("password").toString();
        String secretKey = requestMap.get("secretKey").toString();
        String salt = requestMap.get("salt").toString();
        String sign = requestMap.get("sign").toString();
        /*非空校验*/
        if(StringUtils.isEmpty(platfromMarking)|| StringUtils.isEmpty(passWord)
                || StringUtils.isEmpty(secretKey) ||StringUtils.isEmpty(sign)
                ||StringUtils.isEmpty(salt)||StringUtils.isEmpty(username)
        ){
            return String.valueOf(ApiResult.failure("获取token参数为空！"));
        }
        Long expire = 60 * 1000 * 120L;
        /*给用户jwt加密生成token*/
        int time = 60 * 60 * 2;
        String blanceAccessToken = JwtUtils.sign(secretKey, expire).toString();
        redisUtils.delKey(blanceAccessToken);
        redisUtils.setKey(blanceAccessToken, JsonUtils.mapToJson(reqMap), time);
        /*封装成对象返回给客户端*/
        reqMap.put("tgAccessToken", blanceAccessToken);
        reqMap.put("time", time);
        System.out.println("-------"+JsonUtils.beanToJson(ApiResult.success(reqMap)));
        return JsonUtils.beanToJson(ApiResult.success(reqMap));
    }
    /**
     * 获取十六进制字符串形式的MD5摘要
     */
    public static String md5Hex(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bs = md5.digest(src.getBytes());
            return new String(new Hex().encode(bs));
        } catch (Exception e) {
            return null;
        }
    }

}

